var InfoApi = require('../../api/information.js')
var util = require('../../utils/util')

Page({
  data: {
    currentMenuName: '铜',
    currentPageId: '1713818998033260545',
    pageList: [
      {itemName: '铜', pageId: '1713818998033260545'},
      {itemName: '铝', pageId: '1713819084322676737'},
      {itemName: '锌', pageId: '1713819185438957569'},
      {itemName: '铅', pageId: '1713819279378784257'},
      {itemName: '锡', pageId: '1713819359552905217'},
      {itemName: '镍', pageId: '1713819446073008130'},
    ],
    pagination: {
      current: 1,
      pageSize: 20,
      total: 0
    },
    pageInfo: {},
    tableData: [],
    hasMore: false,
  },
  onShow() {
    if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
    this.initData()
    setTimeout(()=>{
      this.getFrontLatestArticleList()
    }, 200)
  },
  onHide(){
    this.initData()
  },
  initData(){
    this.setData({
      currentMenuName: '铜',
      currentPageId: '1713818998033260545',
    })
  },
  // 查询前端最新文章组件
  getFrontLatestArticleList(){
    InfoApi.getFrontLatestArticleList({pageId: this.data.currentPageId, locationId: '1714197680119816193'}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (res.data && res.data.length > 0) {
          this.setData({
            pageInfo: res.data[0]
          })
          this.getFrontLatestArticleNewsPage(res.data[0].latestArticleId)
        } else {
          this.setData({
            pageInfo: {},
            tableData: [],
            hasMore: false,
          })
        }
      }
    })
  },
  // 查询前端最新文章组件新闻
  getFrontLatestArticleNewsPage(_latestArticleId){
    InfoApi.getFrontLatestArticleNewsPage({latestArticleId: _latestArticleId, pageNum: this.data.pagination.current, pageSize: this.data.pagination.pageSize}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        let array = [];
        if (res.data.data && res.data.data.length > 0) {
          array = res.data.data.map(item=>{
            item.publishTime = item.publishTime ? util.getDateDiff(item.publishTime) : '';
            return item;
          })
        }
        if (this.data.pagination.current == 1) {
          this.setData({
            tableData: array
          })
        } else {
          this.setData({
            tableData: this.data.tableData.concat(array)
          })
        }
        this.setData({
          ['pagination.total']: res.data.count,
          hasMore: res.data.pages > this.data.pagination.current ? true : false
        })
      }
    })
  },
  // 点击菜单
  menuClick(e){
    let current = e.currentTarget.dataset.item;
    this.setData({
      currentMenuName: current.itemName,
      currentPageId: current.pageId,
      ['pagination.current']: 1,
    })
    this.getFrontLatestArticleList()
  },
  // 查看新闻详情
  checkDetail(e){
    let currentItem = e.currentTarget.dataset.item;
    wx.setStorageSync('newsInfo', currentItem)
    wx.navigateTo({
      url: '/packageA/pages/newsDetail/index',
    })
  },
  // 加载更多
  showMore(){
    var pageNum = this.data.pagination.current;
    this.setData({
      ['pagination.current']: pageNum+1
    })
    this.getFrontLatestArticleList(this.data.pageInfo.latestArticleId)
  }
})